package com.nanjia.boot.warehouse.storagelocation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.warehouse.storagelocation.model.form.WarehouseStorageLocationForm;
import com.nanjia.boot.warehouse.storagelocation.model.query.WarehouseStorageLocationQuery;
import com.nanjia.boot.warehouse.storagelocation.model.vo.WarehouseStorageLocationVO;
import com.nanjia.boot.warehouse.storagelocation.service.WarehouseStorageLocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 仓库库位前端控制层
 *
 * @author 周洋
 * @since 2025-08-22 09:39
 */
@Tag(name = "仓库库位接口")
@RestController
@RequestMapping("/api/v1/warehouse-storage-location")
@RequiredArgsConstructor
public class WarehouseStorageLocationController {

    private final WarehouseStorageLocationService warehouseStorageLocationService;

    @Operation(summary = "仓库库位分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('storage_location:warehouse-storage-location:query')")
    public PageResult<WarehouseStorageLocationVO> getWarehouseStorageLocationPage(WarehouseStorageLocationQuery queryParams) {
        IPage<WarehouseStorageLocationVO> result = warehouseStorageLocationService.getWarehouseStorageLocationPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增仓库库位")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('storage_location:warehouse-storage-location:add')")
    public Result<Void> saveWarehouseStorageLocation(@RequestBody @Valid WarehouseStorageLocationForm formData, HttpServletRequest request) {
        boolean result = warehouseStorageLocationService.saveWarehouseStorageLocation(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取仓库库位表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('storage_location:warehouse-storage-location:edit')")
    public Result<WarehouseStorageLocationForm> getWarehouseStorageLocationForm(
            @Parameter(description = "仓库库位ID") @PathVariable Long id
    ) {
        WarehouseStorageLocationForm formData = warehouseStorageLocationService.getWarehouseStorageLocationFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改仓库库位")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('storage_location:warehouse-storage-location:edit')")
    public Result<Void> updateWarehouseStorageLocation(
            @Parameter(description = "仓库库位ID") @PathVariable Long id,
            @RequestBody @Validated WarehouseStorageLocationForm formData, HttpServletRequest request
    ) {
        boolean result = warehouseStorageLocationService.updateWarehouseStorageLocation(id, formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除仓库库位")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('storage_location:warehouse-storage-location:delete')")
    public Result<Void> deleteWarehouseStorageLocations(
            @Parameter(description = "仓库库位ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = warehouseStorageLocationService.deleteWarehouseStorageLocations(ids);
        return Result.judge(result);
    }
}
